package mylogger

import (
	"fmt"
	"time"
)

type ConsoleLogger struct {
	Level Loglevel
}

func NewConsoleLogger(loglevelstring string) ConsoleLogger {
	level, err := parse(loglevelstring)
	if err != nil {

		panic(err)
	}

	return ConsoleLogger{Level: level}
}
func (c ConsoleLogger) enableLog(loglevelstring string) (bool, string) {
	ls, err := parse(loglevelstring)
	if err != nil {
		fmt.Println(err)
	}

	if c.Level > DEBUG && ls >= c.Level {

		return true, loglevelstring
	} else if c.Level == DEBUG {
		return true, loglevelstring
	}
	return false, loglevelstring
}

func (c ConsoleLogger) log(loglevelstring, format string, a ...interface{}) {
	flag, s := c.enableLog(loglevelstring)
	if flag {
		t := time.Now().Format("2006/01/02/15:04:05")
		msg := fmt.Sprintf(format, a...)
		fmt.Printf("%s [%s]  %s\n ", t, s, msg)
	}
}
func (c ConsoleLogger) INFO(format string, a ...interface{}) {

	c.log("info", format, a...)

}
func (c ConsoleLogger) DEBUG(format string, a ...interface{}) {

	c.log("debug", format, a...)
}
func (c ConsoleLogger) TRACE(format string, a ...interface{}) {

	c.log("trace", format, a...)
}
func (c ConsoleLogger) WARNING(format string, a ...interface{}) {

	c.log("warning", format, a...)
}
func (c ConsoleLogger) ERROR(format string, a ...interface{}) {

	c.log("error", format, a...)
}
func (c ConsoleLogger) FATAL(format string, a ...interface{}) {

	c.log("fatal", format, a...)
}
